package cn.yuanqiao.archive.supervision.service.impl;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.supervision.domain.SupervisionFile;
import cn.yuanqiao.archive.supervision.mapper.SupervisionFileMapper;
import cn.yuanqiao.archive.supervision.service.ISupervisionFileService;
import cn.yuanqiao.archive.wenjianzhengli.utils.FileUploadUtils;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.exception.file.FileNameLengthLimitExceededException;
import cn.yuanqiao.common.exception.file.InvalidExtensionException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.file.FileTypeUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 督察指导附件Service业务层处理
 * 
 * @author xy
 * @date 2023-08-01
 */
@Service
public class SupervisionFileServiceImpl implements ISupervisionFileService
{
    @Autowired
    private SupervisionFileMapper supervisionFileMapper;

    @Autowired
    private ITenantSysConfigService sysConfigService;

    /**
     * 查询督察指导附件
     * 
     * @param id 督察指导附件主键
     * @return 督察指导附件
     */
    @Override
    public SupervisionFile selectSupervisionFileById(Long id)
    {
        return supervisionFileMapper.selectSupervisionFileById(id,new SupervisionFile().getTableName());
    }

    /**
     * 查询督察指导附件列表
     * 
     * @param supervisionFile 督察指导附件
     * @return 督察指导附件
     */
    @Override
    public List<SupervisionFile> selectSupervisionFileList(SupervisionFile supervisionFile)
    {
        return supervisionFileMapper.selectSupervisionFileList(supervisionFile);
    }

    /**
     * 新增督察指导附件
     * 
     * @param supervisionFile 督察指导附件
     * @return 结果
     */
    @Override
    public int insertSupervisionFile(SupervisionFile supervisionFile)
    {
        supervisionFile.setCreateTime(DateUtils.getNowDate());
        return supervisionFileMapper.insertSupervisionFile(supervisionFile);
    }

    /**
     * 修改督察指导附件
     * 
     * @param supervisionFile 督察指导附件
     * @return 结果
     */
    @Override
    public int updateSupervisionFile(SupervisionFile supervisionFile)
    {
        return supervisionFileMapper.updateSupervisionFile(supervisionFile);
    }

    /**
     * 批量删除督察指导附件
     * 
     * @param ids 需要删除的督察指导附件主键
     * @return 结果
     */
    @Override
    public int deleteSupervisionFileByIds(Long[] ids)
    {
        return supervisionFileMapper.deleteSupervisionFileByIds(ids,new SupervisionFile().getTableName());
    }

    /**
     * 删除督察指导附件信息
     * 
     * @param id 督察指导附件主键
     * @return 结果
     */
    @Override
    public int deleteSupervisionFileById(Long id)
    {
        return supervisionFileMapper.deleteSupervisionFileById(id);
    }

    @Override
    public SupervisionFile uploadFile(Long id, MultipartFile file) throws InvalidExtensionException, IOException {
        LoginUser user = SecurityUtils.getLoginUser();
        String uplodPath = sysConfigService.selectConfigByKey("YQ_MNT");
        if(StringUtils.isEmpty(uplodPath)){
            throw new ServiceException("系统参数错误YQ_MNT未找到配置！");
        }

            //生成地址
            String maxSize = sysConfigService.selectConfigByKey("DA_FILE_MAX");
            String extensions = sysConfigService.selectConfigByKey("DA_ALLOWED_EXTENSION");
            int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
            if (fileNamelength > 50) {
                throw new FileNameLengthLimitExceededException(50);
            }
            FileUploadUtils.setDefaultConfig(maxSize, extensions);
            FileUploadUtils.fileAllowed(file);
            String filePath="supervision";
            String fileName=new Date().getTime()+"."+FileTypeUtils.getExtension(file);
            String absPath = getAbsoluteFile(uplodPath+filePath, fileName).getAbsolutePath();
            file.transferTo(Paths.get(absPath));
            String path=filePath+File.separator+fileName;
            SupervisionFile supervisionFile = new SupervisionFile();
            supervisionFile.setSupervisionId(id);
            supervisionFile.setCreateId(SecurityUtils.getUserId());
            supervisionFile.setPath(path);
            supervisionFile.setFileSize(String.valueOf(file.getSize()));
            supervisionFile.setDocMnt(uplodPath);
            supervisionFile.setDocName(file.getOriginalFilename());
            supervisionFile.setDocType( FileTypeUtils.getExtension(file).toUpperCase());
            supervisionFile.setRealDocName(fileName);
            supervisionFile.setCreateTime(DateUtils.getNowDate());
            supervisionFile.setIsDel(0);
            supervisionFileMapper.insertSupervisionFile(supervisionFile);

            return supervisionFile;

    }

    private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
    }
}
